package com.loyin.tool;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * 分析url
 * 
 * @author 刘声凤 2011-11-11
 */
public class AnalysisUrl
{
	private Logger log = Logger.getLogger(this.getClass());
	public int mincount = 3;

	/**
	 * 分析url
	 * 
	 * @param url
	 * @param keyword
	 * @throws IOException
	 * @throws MalformedURLException
	 */
	public List<Map<String, Object>> analysis(String url, String keyword) throws Exception
	{
		URI baseUri = new URI(url);// 基本网页URI
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		String[] keywords = keyword.split(" ");
		Document doc = Jsoup.parse(new URL(url), 5000);
		Elements alinklist = doc.getElementsByTag("a");
		for (Element a : alinklist)
		{
			String title = a.text();
			int count = 0;
			for (String kw : keywords)
			{
				if (title.contains(kw))
				{
					count++;
				}
			}
			if (count >= mincount)
			{
				String href = a.attr("href");
				Map<String, Object> map = new HashMap<String, Object>();
				URI hrefUri = baseUri.resolve(href);// 解析于上述网页的相对URL，得到绝对URI
				URL hrefURL = hrefUri.toURL();// 转成URL
				map.put("title", title);
				map.put("href", hrefURL.toString());
				map.put("count", count);
				list.add(map);
			}
		}
		return list;
	}

	public static void main(String[] args)
	{
		try
		{
			List<Map<String, Object>> list = new AnalysisUrl().analysis("http://www.bjcoc.gov.cn/tzgg3/", "年度 立项 批 名单 项目 通知 企业 科技");
			for (Map<String, Object> map : list)
			{
				System.out.println(map.get("title") + "\t" + map.get("count") + "\t" + map.get("href"));
			}
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}
